import 'package:dmz/base/pageWidget/base_stateful_widget.dart';
import 'package:dmz/business/mine/controller/seting_repair_controller.dart';
import 'package:dmz/utils/sp_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../base/widget/defult_widget.dart';

import '../../home/model/serve_point.dart';
import '../model/repair_model.dart';
import 'garage_widgets.dart';
import 'seting_widgets.dart';

class SetRepairWidgets {
  static repairTopWidget(int select, Function(int) click) {
    List<String> titles = ['全部'.tr, '待接单'.tr, '进行中'.tr, '已完成'.tr];
    item(title, tag, select) {
      return Container(
          alignment: Alignment.center,
          width: 190.sp,
          child: Column(children: [
            Container(
                alignment: Alignment.center,
                height: 120.sp,
                child: DefaultText(title, select ? 40.sp : 28.sp,
                    textColor: select
                        ? const Color(0xFF23C565)
                        : const Color(0xFF8F8F8F),
                    fontWeight: select ? FontWeight.w700 : FontWeight.normal)),
            Container(
                height: 5.sp,
                color: select ? const Color(0xFF23C565) : Colors.transparent)
          ]));
    }

    return Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: List.generate(
                titles.length,
                (index) => item(titles[index], index, select == index)
                    .addGestureDetector(() => click(index))))
        .marginOnly(left: 32.sp, right: 32.sp);
  }

  static Widget repairPageWidget(RepairModel data) {
    return Container(
            color: const Color(0xFF434650),
            child: Column(children: [
              Row(children: [
                DefaultText('售后服务维修单'.tr, 48.sp,
                    textColor: const Color(0xFFF9F9F9),
                    fontWeight: FontWeight.w700),
                const Expanded(child: SizedBox()),
                Row(children: [
                  if ((data.status ?? 0) < 3)
                    Image.asset(
                            'assets/images/user/repair_type${(data.status ?? 0) + 1}.png',
                            width: 64.sp,
                            height: 64.sp)
                        .marginOnly(right: 20.sp),
                  DefaultText(data.getStatusStr(), 28.sp,
                          textColor: const Color(0xFFF9F9F9))
                      .marginOnly(right: 16.sp),
                  Image.asset('assets/images/home/set/right_icon.png',
                      width: 18.sp, height: 32.sp)
                ])
              ]).paddingOnly(
                  left: 32.sp, right: 32.sp, top: 32.sp, bottom: 30.sp),
              Container(height: 2.sp, color: const Color(0xFFD8D8D8)),
              Row(children: [
                DefaultNetWorkImage(data.ebike_type_image ?? '',
                    width: 171.sp, height: 134.sp),
                Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                  DefaultText(data.ebike_type_name, 48.sp,
                          fontWeight: FontWeight.w700,
                          textColor: const Color(0xFFF9F9F9))
                      .marginOnly(bottom: 20.sp),
                  DefaultText('上报时间'.tr, 28.sp,
                      textColor: const Color(0xFF999999)),
                  DefaultText(
                      SPUtils.timeIntToTimeStr(data.updated_at ?? 0), 28.sp,
                      textColor: const Color(0xFFF4F4F4))
                ]).marginOnly(left: 34.sp)
              ]).marginOnly(bottom: 40.sp, top: 42.sp)
            ]))
        .didClipRRectAll(56.sp)
        .marginOnly(bottom: 32.sp, left: 32.sp, right: 32.sp);
  }

  static Widget addRepairTopWidget(List<String> contents, Function(int) click) {
    List<String> titles = [
      '车辆选择'.tr,
      '网点名称'.tr,
      '维修时间'.tr,
    ];

    return Container(
        color: const Color(0xFF434650),
        child: Column(
            mainAxisSize: MainAxisSize.min,
            children: List.generate(
                titles.length,
                (index) => SetingWidgets.pageItem(index, titles[index],
                    content: contents[index],
                    hitLine: index == 0,
                    click: click))));
  }

  static Widget inputWidget(
      TextEditingController controller, Function(String) valueChange) {
    return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
      Container(height: 2.sp, color: const Color(0xFFD8D8D8))
          .marginOnly(top: 70.sp, bottom: 70.sp),
      DefaultText('文字说明'.tr, 32.sp, textColor: const Color(0xB3FFFFFF))
          .marginOnly(bottom: 54.sp),
      SetingWidgets.inputWidget(controller, valueChange, inputLenth: 50),
      Container(height: 2.sp, color: const Color(0xFFD8D8D8))
          .marginOnly(top: 80.sp),
    ]).marginOnly(left: 32.sp, right: 32.sp);
  }

  static selectSeverPointWidget(
      List<ServePoint> points, Function() clickSelect) {
    pointItem(int index, {bool hitLine = false}) {
      return GetBuilder<SetingRepairController>(
          id: 'severList',
          builder: (controller) {
            return Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  if (!hitLine)
                    Container(height: 2.sp, color: const Color(0xFFD8D8D8)),
                  Row(children: [
                    Image.asset(
                            'assets/images/home/share/select_${controller.opintSelTypeNum == index ? 'true' : 'false'}.png',
                            width: 64.sp,
                            height: 64.sp)
                        .addGestureDetector(
                            () => controller.pointListSelect(index))
                        .marginOnly(right: 16.sp),
                    Expanded(
                        child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                          DefaultText(points[index].name, 40.sp,
                              textColor: const Color(0xFFF9F9F9),
                              fontWeight: FontWeight.w700),
                          DefaultText(
                            points[index].addr,
                            28.sp,
                            textColor: const Color(0xFFF9F9F9),
                          ),
                        ])),
                    DefaultText(
                      '${(points[index].distance ?? 0) ~/ 1000}km',
                      32.sp,
                      textColor: const Color(0xFFF9F9F9),
                    ).marginOnly(left: 20.sp),
                  ]).marginOnly(top: 40.sp, bottom: 40.sp),
                ]).marginOnly(left: 32.sp, right: 32.sp);
          });
    }

    return Column(children: [
      Container(
          constraints: BoxConstraints(maxHeight: 1000.sp),
          child: ListView.builder(
              padding: const EdgeInsets.only(top: 0),
              shrinkWrap: true,
              itemCount: points.length,
              itemBuilder: (context, index) {
                return pointItem(index, hitLine: index == 0);
              }).marginOnly(bottom: 30.sp)),
      DefaultBtn(click: () => clickSelect(), title: '确认'.tr)
          .marginOnly(bottom: 112.sp)
    ]);
  }
}

class RepairListWidget extends StatefulWidget {
  final int tag;
  const RepairListWidget({super.key, required this.tag});

  @override
  State<RepairListWidget> createState() => _RepairListWidgetState();
}

class _RepairListWidgetState extends State<RepairListWidget> {
  final ScrollController _scrollController = ScrollController();
  bool _isLoadingMore = false;
  int index = 1;

  Future<void> _loadMore() async {
    var controller = Get.find<SetingRepairController>();
    if (_isLoadingMore) return;
    if ((controller.pagedatas[widget.tag].length) % 20 > 0) return;

    _isLoadingMore = true;
    index++;
    await controller.getListData(index, widget.tag, isAdd: true);
    _isLoadingMore = false;
  }

  @override
  void initState() {
    super.initState();
    _scrollController.addListener(_scrollListener);
  }

  void _scrollListener() {
    if (_scrollController.position.extentAfter < 500) {
      _loadMore();
    }
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SetingRepairController>(
        id: 'repair${widget.tag}',
        builder: (controller) {
          return RefreshIndicator(
                  color: const Color(0xFF999999),
                  onRefresh: () => controller.getListData(1, widget.tag),
                  child: controller.pagedatas[widget.tag].isEmpty
                      ? Center(child: GarageWidgets.emptyWidget())
                      : ListView.builder(
                          itemCount: controller.pagedatas[widget.tag].length,
                          itemBuilder: (context, index) {
                            return SetRepairWidgets.repairPageWidget(
                                controller.pagedatas[widget.tag][index]);
                          }))
              .marginOnly(top: 48.sp);
        });
  }
}
